/**
 * 
 */
package com.appspot.newsographer.client;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.ArrayUtils;

import sun.awt.SunHints.Value;

import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.maps.client.InfoWindow;
import com.google.gwt.maps.client.InfoWindowContent;
import com.google.gwt.maps.client.MapWidget;
import com.google.gwt.maps.client.Maps;
import com.google.gwt.maps.client.control.LargeMapControl;
import com.google.gwt.maps.client.event.MarkerClickHandler;
import com.google.gwt.maps.client.geom.LatLng;
import com.google.gwt.maps.client.geom.LatLngBounds;
import com.google.gwt.maps.client.geocode.Geocoder;
import com.google.gwt.maps.client.geocode.LatLngCallback;
import com.google.gwt.maps.client.overlay.Marker;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.Window;
import com.google.gwt.maps.client.InfoWindow;
import com.google.gwt.maps.client.InfoWindowContent;




/**
 * 
 * @author anuradha.uduwage
 *
 */
public class GWTMaxiMap extends Composite {
	
	private VerticalPanel verticalPanel;
	private HashMap<String, Integer> tags;
	
	public GWTMaxiMap() {
	    verticalPanel = new VerticalPanel();
	    initWidget(verticalPanel);
	    verticalPanel.setWidth("800px");
	    verticalPanel.setHeight("100%");
	    
	    HTML htmlMapDemo = new HTML("Maps with Locations", true);
	    verticalPanel.add(htmlMapDemo);
	    tags = new HashMap<String, Integer>();
	    tags.put("Afghanistan", 0);
	    tags.put("Albania", 0);
	    tags.put("Algeria", 0);
	    tags.put("American Samoa", 0);
	    tags.put("Andorra", 0);
	    tags.put("Angola", 0);
	    tags.put("Anguilla", 0);
	    tags.put("Antarctica", 0);
	    tags.put("Antigua and Barbuda", 0);
	    tags.put("Argentina", 0);
	    tags.put("Armenia", 0);
	    tags.put("Aruba", 0);
	    tags.put("Australia", 0);
	    tags.put("Austria", 0);
	    tags.put("Azerbaijan", 0);
	    tags.put("Bahamas", 0);
	    tags.put("Bahrain", 0);
	    tags.put("Bangladesh", 0);
	    tags.put("Barbados", 0);
	    tags.put("Belarus", 0);
	    tags.put("Belgium", 0);
	    tags.put("Belize", 0);
	    tags.put("Benin", 0);
	    tags.put("Bermuda", 0);
	    tags.put("Bhutan", 0);
	    tags.put("Bolivia", 0);
	    tags.put("Bosnia and Herzegovina", 0);
	    tags.put("Botswana", 0);
	    tags.put("Bouvet Island", 0);
	    tags.put("Brazil", 0);
	    tags.put("British Indian Ocean territory", 0);
	    tags.put("Brunei Darussalam", 0);
	    tags.put("Bulgaria", 0);
	    tags.put("Burkina Faso", 0);
	    tags.put("Burundi", 0);
	    tags.put("Cambodia", 0);
	    tags.put("Cameroon", 0);
	    tags.put("Canada", 0);
	    tags.put("Cape Verde", 0);
	    tags.put("Cayman Islands", 0);
	    tags.put("Central African Republic", 0);
	    tags.put("Chad", 0);
	    tags.put("Chile", 0);
	    tags.put("China", 0);
	    tags.put("Christmas Island", 0);
	    tags.put("Cocos (Keeling) Islands", 0);
	    tags.put("Colombia", 0);
	    tags.put("Comoros", 0);
	    tags.put("Congo", 0);
	    tags.put("Congo", 0);
	    tags.put(" Democratic Republic", 0);
	    tags.put("Cook Islands", 0);
	    tags.put("Costa Rica", 0);
	    tags.put("C&ocirc;te d&#39;Ivoire (Ivory Coast)", 0);
	    tags.put("Croatia (Hrvatska)", 0);
	    tags.put("Cuba", 0);
	    tags.put("Cyprus", 0);
	    tags.put("Czech Republic", 0);
	    tags.put("Denmark", 0);
	    tags.put("Djibouti", 0);
	    tags.put("Dominica", 0);
	    tags.put("Dominican Republic", 0);
	    tags.put("East Timor", 0);
	    tags.put("Ecuador", 0);
	    tags.put("Egypt", 0);
	    tags.put("El Salvador", 0);
	    tags.put("Equatorial Guinea", 0);
	    tags.put("Eritrea", 0);
	    tags.put("Estonia", 0);
	    tags.put("Ethiopia", 0);
	    tags.put("Falkland Islands", 0);
	    tags.put("Faroe Islands", 0);
	    tags.put("Fiji", 0);
	    tags.put("Finland", 0);
	    tags.put("France", 0);
	    tags.put("French Guiana", 0);
	    tags.put("French Polynesia", 0);
	    tags.put("French Southern Territories", 0);
	    tags.put("Gabon", 0);
	    tags.put("Gambia", 0);
	    tags.put("Georgia", 0);
	    tags.put("Germany", 0);
	    tags.put("Ghana", 0);
	    tags.put("Gibraltar", 0);
	    tags.put("Greece", 0);
	    tags.put("Greenland", 0);
	    tags.put("Grenada", 0);
	    tags.put("Guadeloupe", 0);
	    tags.put("Guam", 0);
	    tags.put("Guatemala", 0);
	    tags.put("Guinea", 0);
	    tags.put("Guinea-Bissau", 0);
	    tags.put("Guyana", 0);
	    tags.put("Haiti", 0);
	    tags.put("Heard and McDonald Islands", 0);
	    tags.put("Honduras", 0);
	    tags.put("Hong Kong", 0);
	    tags.put("Hungary", 0);
	    tags.put("Iceland", 0);
	    tags.put("India", 0);
	    tags.put("Indonesia", 0);
	    tags.put("Iran", 0);
	    tags.put("Iraq", 0);
	    tags.put("Ireland", 0);
	    tags.put("Israel", 0);
	    tags.put("Italy", 0);
	    tags.put("Jamaica", 0);
	    tags.put("Japan", 0);
	    tags.put("Jordan", 0);
	    tags.put("Kazakhstan", 0);
	    tags.put("Kenya", 0);
	    tags.put("Kiribati", 0);
	    tags.put("Korea (north)", 0);
	    tags.put("Korea (south)", 0);
	    tags.put("Kuwait", 0);
	    tags.put("Kyrgyzstan", 0);
	    tags.put("Lao People&#39;s Democratic Republic", 0);
	    tags.put("Latvia", 0);
	    tags.put("Lebanon", 0);
	    tags.put("Lesotho", 0);
	    tags.put("Liberia", 0);
	    tags.put("Libya", 0);
	    tags.put("Libyan Arab Jamahiriya", 0);
	    tags.put("Liechtenstein", 0);
	    tags.put("Lithuania", 0);
	    tags.put("Luxembourg", 0);
	    tags.put("Macao", 0);
	    tags.put("Macedonia", 0);
	    tags.put("Madagascar", 0);
	    tags.put("Malawi", 0);
	    tags.put("Malaysia", 0);
	    tags.put("Maldives", 0);
	    tags.put("Mali", 0);
	    tags.put("Malta", 0);
	    tags.put("Marshall Islands", 0);
	    tags.put("Martinique", 0);
	    tags.put("Mauritania", 0);
	    tags.put("Mauritius", 0);
	    tags.put("Mayotte", 0);
	    tags.put("Mexico", 0);
	    tags.put("Micronesia", 0);
	    tags.put("Moldova", 0);
	    tags.put("Monaco", 0);
	    tags.put("Mongolia", 0);
	    tags.put("Montserrat", 0);
	    tags.put("Morocco", 0);
	    tags.put("Mozambique", 0);
	    tags.put("Myanmar", 0);
	    tags.put("Namibia", 0);
	    tags.put("Nauru", 0);
	    tags.put("Nepal", 0);
	    tags.put("Netherlands", 0);
	    tags.put("Netherlands Antilles", 0);
	    tags.put("New Caledonia", 0);
	    tags.put("New Zealand", 0);
	    tags.put("Nicaragua", 0);
	    tags.put("Niger", 0);
	    tags.put("Nigeria", 0);
	    tags.put("Niue", 0);
	    tags.put("Norfolk Island", 0);
	    tags.put("Northern Mariana Islands", 0);
	    tags.put("Norway", 0);
	    tags.put("Oman", 0);
	    tags.put("Pakistan", 0);
	    tags.put("Palau", 0);
	    tags.put("Palestinian Territories", 0);
	    tags.put("Panama", 0);
	    tags.put("Papua New Guinea", 0);
	    tags.put("Paraguay", 0);
	    tags.put("Peru", 0);
	    tags.put("Philippines", 0);
	    tags.put("Pitcairn", 0);
	    tags.put("Poland", 0);
	    tags.put("Portugal", 0);
	    tags.put("Puerto Rico", 0);
	    tags.put("Qatar", 0);
	    tags.put("R&eacute;union", 0);
	    tags.put("Romania", 0);
	    tags.put("Russian Federation", 0);
	    tags.put("Rwanda", 0);
	    tags.put("Saint Helena", 0);
	    tags.put("Saint Kitts and Nevis", 0);
	    tags.put("Saint Lucia", 0);
	    tags.put("Saint Pierre and Miquelon", 0);
	    tags.put("Saint Vincent and the Grenadines", 0);
	    tags.put("Samoa", 0);
	    tags.put("San Marino", 0);
	    tags.put("Sao Tome and Principe", 0);
	    tags.put("Saudi Arabia", 0);
	    tags.put("Senegal", 0);
	    tags.put("Serbia and Montenegro", 0);
	    tags.put("Seychelles", 0);
	    tags.put("Sierra Leone", 0);
	    tags.put("Singapore", 0);
	    tags.put("Slovakia", 0);
	    tags.put("Slovenia", 0);
	    tags.put("Solomon Islands", 0);
	    tags.put("Somalia", 0);
	    tags.put("South Africa", 0);
	    tags.put("South Georgia and the South Sandwich Islands", 0);
	    tags.put("Spain", 0);
	    tags.put("Sri Lanka", 0);
	    tags.put("Sudan", 0);
	    tags.put("Suriname", 0);
	    tags.put("Svalbard and Jan Mayen Islands", 0);
	    tags.put("Swaziland", 0);
	    tags.put("Sweden", 0);
	    tags.put("Switzerland", 0);
	    tags.put("Syria", 0);
	    tags.put("Taiwan", 0);
	    tags.put("Tajikistan", 0);
	    tags.put("Tanzania", 0);
	    tags.put("Thailand", 0);
	    tags.put("Togo", 0);
	    tags.put("Tokelau", 0);
	    tags.put("Tonga", 0);
	    tags.put("Trinidad and Tobago", 0);
	    tags.put("Tunisia", 0);
	    tags.put("Turkey", 0);
	    tags.put("Turkmenistan", 0);
	    tags.put("Turks and Caicos Islands", 0);
	    tags.put("Tuvalu", 0);
	    tags.put("Uganda", 0);
	    tags.put("Ukraine", 0);
	    tags.put("United Arab Emirates", 0);
	    tags.put("United Kingdom", 0);
	    tags.put("United States of America", 0);
	    tags.put("Uruguay", 0);
	    tags.put("Uzbekistan", 0);
	    tags.put("Vanuatu", 0);
	    tags.put("Vatican City", 0);
	    tags.put("Venezuela", 0);
	    tags.put("Vietnam", 0);
	    tags.put("Virgin Islands (British)", 0);
	    tags.put("Virgin Islands (US)", 0);
	    tags.put("Wallis and Futuna Islands", 0);
	    tags.put("Western Sahara", 0);
	    tags.put("Yemen", 0);
	    tags.put("Zaire", 0);
	    tags.put("Zambia", 0);
	    tags.put("Zimbabwe", 0);
	}
	
	public void draw(HashMap<String, ArrayList<String>> result) {
		Set<String> titles = result.keySet();
		String[] keys = new String[titles.size()];
		titles.toArray(keys);
		for(int i=0; i < keys.length; i++)
		{
			ArrayList<String> article_tags = result.get(keys[i]);
			for(int j=1; j < article_tags.size(); j++) {
				if(tags.get(article_tags.get(j)) != null) {
					tags.put(article_tags.get(j), tags.get(article_tags.get(j)) + 1);
				}
			}
		}
		Maps.loadMapsApi("ABQIAAAAWtUmPYFDQu-KEPMwKcmJxRR4PHjGfplgKTSb8HFushBDBVU3SxR0EoOeyG3eJ8HDmAgMNalhCdqamA", 
				"2", false, new Runnable() {
			public void run() {		
				constructMap();
			}
		});
	}
	
	private Geocoder geocoder;

	private void constructMap() {

		final MapWidget map = new MapWidget(LatLng.newInstance(0, 0), 2);
		map.setDoubleClickZoom(true);
		map.setDraggable(true);
		map.setSize("800px", "550px");
		map.addControl(new LargeMapControl());

	    Iterator it = tags.entrySet().iterator();
	    while (it.hasNext()) {
	        Map.Entry pairs = (Map.Entry)it.next();
	        System.out.println(pairs.getKey() + " = " + pairs.getValue());
	        if((Integer) pairs.getValue() > 0) {
	        	final Integer freq = (Integer) pairs.getValue();
	        	geocoder = new Geocoder();
	    	    final InfoWindow info = map.getInfoWindow();
	    	    geocoder.getLatLng((String) pairs.getKey(), new LatLngCallback() {
	    	        public void onFailure() {
	    	        }
	    	        
	    	        public void onSuccess(LatLng point) {
	    	            final Marker marker = new Marker(point);
	    	            final Integer inner_freq = freq;
	    	            map.addOverlay(marker);
	    	            info.open(marker, new InfoWindowContent("Frequency = ".concat(freq.toString())));
	    	            marker.addMarkerClickHandler(new MarkerClickHandler() {
	    	                public void onClick(MarkerClickEvent event) {
	    	                  InfoWindow info = map.getInfoWindow();
	    	                  info.open(marker, new InfoWindowContent("Frequency = ".concat(inner_freq.toString())));
	    	                }
	    	              });

	    	          }
	    	        });
	        }
	    }


	    




		final DockLayoutPanel dock = new DockLayoutPanel(Unit.PX);
		dock.addNorth(map, 550);

		// Add the map to the HTML host page
		getVerticalPanel().add(dock);
	}
	
	

	public VerticalPanel getVerticalPanel() {
		return verticalPanel;
	}

}
